
import java.io.*;

/*
ID: huangmingbo
LANG: JAVA
TASK: skidesign
 */
public class skidesign {
    public static void main(String[] args) throws IOException {

        try (
                BufferedReader reader = new BufferedReader(new InputStreamReader(skidesign.class.getResourceAsStream(skidesign.class.getSimpleName() + ".in")));
                PrintWriter writer = new PrintWriter(new FileWriter(skidesign.class.getSimpleName() + ".out"));
        ) {
            int N = Integer.parseInt(reader.readLine());
            int[] hills = new int[N];
            for (int i = 0; i < N; i++) {
                hills[i] = Integer.parseInt(reader.readLine());
            }

            bubbleSort(hills, N);

            int cost = Integer.MAX_VALUE, tmpc, max, diff;
            for (int i = 0; i <= hills[N - 1] - 17; i++) {
                max = i + 17;
                tmpc = 0;

                int j = 0;
                for (; j < N && hills[j] < i; j++) {
                    diff = i - hills[j];
                    tmpc += diff * diff;
                }

                int k = N - 1;
                for (; k >= j && hills[k] > max; k--) {
                    diff = hills[k] - max;
                    tmpc += diff * diff;
                }

                if (cost > tmpc) {
                    cost = tmpc;
                }

            }


            writer.println(cost);
        }


    }

    private static void bubbleSort(int[] hills, int N) {
        int tmp;
        boolean swap;
        for (int i = N - 1; i >= 0; i--) {
            swap = false;
            for (int j = 0; j < i; j++) {
                if (hills[j] > hills[j + 1]) {
                    tmp = hills[j];
                    hills[j] = hills[j + 1];
                    hills[j + 1] = tmp;
                    swap = true;
                }

            }

            // if no element been swap then bubble sort done
            if (!swap) {
                break;
            }
        }
    }
}

